<?php

require_once 'phpmoko/utils/Curl.php';

class Console
{
	private static function consoleErrorHandle($errno, $errstr, $errfile, $errline)
	{
		$curl = new Curl('http://www.phpmoko.com/logerrors/log.php');
		$curl->setParameter('errstr', $errstr, 'POST');
		$curl->setParameter('errfile', $errfile, 'POST');
		$curl->setParameter('errline', $errline, 'POST');
		$hostname = gethostname();
		$curl->setParameter('servername', (isset($hostname)? $hostname : 'Unknow'), 'POST');

		$production = file_exists($_SERVER['DOCUMENT_ROOT'].'/.pmkprod')?'Y':'N';
		$curl->setParameter('production', $production, 'POST');

		$backtrace = debug_backtrace();
		array_shift($backtrace);
		$backtracestr = @json_encode($backtrace);
		$curl->setParameter('backtrace', $backtracestr, 'POST');
		
		$other = '';
		if (isset($_SERVER["HTTP_REFERER"]))
			$other .= 'HTTP REFERER: '.$_SERVER["HTTP_REFERER"]."\n";
		if (isset($_SERVER["SERVER_NAME"]))
			$other .= 'SERVER NAME: '.$_SERVER["SERVER_NAME"]."\n";
		$curl->setParameter('other', $other, 'POST');
		
		$type = '';
		switch ($errno)
		{
			case E_USER_ERROR:
				$type = 'E';
				break;
			case E_USER_WARNING:
				$type = 'W';
				break;
			case E_USER_NOTICE:
				$type = 'N';
				break;
			default:
			case 2048:
				$type = 'I';
				break;
		}
		$curl->setParameter('type', $type, 'POST');
		$curl->exec();
		
		if ($production == 'Y')
			return;
		
		static $id = 0;
		$id++;

		require_once 'phpmoko/ui/RootPanel.php';
		require_once 'phpmoko/externals/JQuery.php';

		$root = RootPanel::get();

		JQuery::activate();

		$span1 = '<span id=\'pmk-error-'.$id.'\' style=\'cursor:pointer; color:blue; text-decoration:underline\' onclick=\'$(this).hide(); $("#pmk-error-full-'.$id.'").show("fast")\'>more</span>';
		$span2 = '<span id=\'pmk-error-full-'.$id.'\' style=\'display: none; font-weight:bold; color: black; cursor:pointer\' onclick=\'$(this).hide(); $("#pmk-error-'.$id.'").show("fast")\'><br>'.$errfile.' ('.$errline.')</span>';

		$message = $errstr.' &nbsp; '.$span1.$span2;

		switch ($errno)
		{
			case E_USER_ERROR:
				$root->addErrorMessage($message);
				break;
			case E_USER_WARNING:
				$root->addWarningMessage($message);
				break;
			case E_USER_NOTICE:
				$root->addDebugMessage($message);
				break;
			default:
			case 2048:
				$root->addInfoMessage($message);
				break;
		}
	}
	
	static function _consoleErrorHandle($errno, $errstr, $errfile, $errline)
	{
		Console::consoleErrorHandle($errno, $errstr, $errfile, $errline);
	}

	static function init()
	{
		set_error_handler("Console::_consoleErrorHandle");
	}

	static function debug($message)
	{
		$callee = current(debug_backtrace());
		Console::consoleErrorHandle(E_USER_NOTICE, $message, $callee['file'], $callee['line']);
	}

	static function error($message)
	{
		$callee = current(debug_backtrace());
		Console::consoleErrorHandle(E_USER_ERROR, $message, $callee['file'], $callee['line']);
	}

	static function warn($message)
	{
		$callee = current(debug_backtrace());
		Console::consoleErrorHandle(E_USER_WARNING, $message, $callee['file'], $callee['line']);
	}

	static function info($message)
	{
		$callee = current(debug_backtrace());
		Console::consoleErrorHandle(2048, $message, $callee['file'], $callee['line']);
	}
}
?>
